from core.basehandler import BaseHandler
from core.room import Room
from core.action import Action
from core.support import get_game
from items.decryptor import Decryptor
import logging

class Binary_Monster(Room):
    def __init__(self, room_id):
        Room.__init__(self, room_id)    # do basic initialization for every room
        
        # build list of actions
        self.add_action(BinarySolo)
        self.add_action(West)
        self.add_action(East)
    
    # return description of room.
    def get_description(self):
        if self.has_translator() and not self.has_decryptor():
            return '''
            Upon entering the room, you see a terrifying (yet somehow simultaneously adorable)
            Binary Monster! Binary Monster says, 'Play me a binary solo!'
            '''
        elif self.has_translator() and self.has_decryptor():
            return '''
            This Binary Monster had a fever... and the only prescription was a binary solo!
            '''
        else:
            return '''
            Upon entering the room, you see a terrifying (yet somehow simultaneously adorable)
            Binary Monster! Binary Monster says, '10011001010010111101!'
            '''
        
    # return image of room
    def get_image(self):
        if self.has_decryptor():
            return 'binary_monster.jpg'
        else:
            return 'binary_monster_unhappy.jpg'
    
    def get_image_width(self):
        return 'width=100%'

    def has_translator(self):
        
        game = get_game()
            
        # does the player have the ASCII translator?
        player = game.get_player()
        
        if "Translator" in player.inventory:
            return True
        # if we get here, player does not have the translator
        return False
    
    def has_decryptor(self):
        
        game = get_game()
            
        # does the player have the ASCII translator?
        player = game.get_player()
        
        if "Decryptor" in player.inventory:
            return True
        # if we get here, player does not have the translator
        return False
    
class Binary_Monster_Handler(BaseHandler):
        
    def get_template(self):
        return 'cpu.html'
    
    # return class to use to create a new instance of this room
    def get_room_class(self):
        return Binary_Monster
    
    def update(self,request_type):
        game = get_game() 
        if hasattr(game,'number'):
            flag = True
            if game.number == "":
                flag = False
            solo = map(str,str(game.number))
            counter = 0
            sololength = len(solo)
            while counter < sololength:
                logging.info(str(type(solo[counter])))
                logging.info(str(type(str('1'))))
                if solo[counter] != '0' and solo[counter] != '1' and solo[counter] != None:
                    flag = False
                    logging.info(solo[counter])
                counter = counter+1
            logging.info(flag)
            if flag == True and not self.get_this_room().has_decryptor():
                self.get_this_room().add_item(Decryptor())
        BaseHandler.update(self, request_type);
    
# define actions for this room
class BinarySolo(Action):
    def __init__(self):
        Action.__init__(self, "BinarySolo")    # do basic initialization for every action
    
    # return description of action (used in label on webpage)
    def get_description(self):
        game = get_game()
        room = game.get_room("Binary_Monster", Binary_Monster)
        if room.has_translator():
            return "Play binary solo"
        else:
            return "110010"
    
    # return id of room to enter when action is complete
    def get_destination(self):
        return "binary_solo"
    
    def is_enabled(self):
        game = get_game()
        room = game.get_room("Binary_Monster", Binary_Monster)
        if room.has_translator():
            return True
        else:
            return False
    
    # return http method to use when user clicks on this action
    # use "get" if just moving to another room.  if changing something
    # like the state of an inventory item or room then use "post"
    def post_method(self):
        return "post"

class West(Action):
    def __init__(self):
        Action.__init__(self, "West")    # do basic initialization for every action
    
    # return description of action (used in label on webpage)
    def get_description(self):
        game = get_game()
        room = game.get_room("Binary_Monster", Binary_Monster)
        if room.has_translator():
            return "Go West"
        else:
            return "10"
    
    # return id of room to enter when action is complete
    def get_destination(self):
        return 'cpu'
    
    # return http method to use when user clicks on this action
    # use "get" if just moving to another room.  if changing something
    # like the state of an inventory item or room then use "post"
    def get_method(self):
        return "get"
    
class East(Action):
    def __init__(self):
        Action.__init__(self, "East")    # do basic initialization for every action
    
    # return description of action (used in label on webpage)
    def get_description(self):
        game = get_game()
        room = game.get_room("Binary_Monster", Binary_Monster)
        if room.has_translator():
            return "Go East"
        else:
            return "01"
    
    # return id of room to enter when action is complete
    def get_destination(self):
        return 'harddrive'
    
    # return http method to use when user clicks on this action
    # use "get" if just moving to another room.  if changing something
    # like the state of an inventory item or room then use "post"
    def get_method(self):
        return "get"
